package com.kee.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kee.server.mapper.AdminRoleMapper;
import com.kee.server.mapper.MenuRoleMapper;
import com.kee.server.mapper.RoleMapper;
import com.kee.server.pojo.RespBean;
import com.kee.server.pojo.Role;
import com.kee.server.service.IRoleService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kee
 * @since 2021-03-22
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private RoleMapper roleMapper;
    @Resource
    private AdminRoleMapper adminRoleMapper;
    @Resource
    private MenuRoleMapper menuRoleMapper;

    /**
     * 更新角色菜单
     * @param rid
     * @param mids
     * @return
     */
    @Override
    public RespBean updateMenuRole(Integer rid, Integer[] mids) {
        //根据角色id删除该角色对应的所有菜单
        super.baseMapper.removeMenuByRids(Collections.singletonList(rid));
        //执行更新
        if(ObjectUtils.isEmpty(mids) ||
                super.baseMapper.updateMenuRole(rid,mids).equals(mids.length)){
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @Override
    public RespBean insertRole(String roleName, String roleNameZh) {
        //判断roleName,roleNameZh非空
        if (null == roleName || roleName.isEmpty() || null == roleNameZh || roleNameZh.isEmpty()) {
            return RespBean.error("角色名不能为空");
        }
        //角色唯一性判断
        if (roleMapper.selectByName(roleName).size() != 0) {
            return RespBean.error("角色英文名已存在，请确认后重新添加");
        }
        if (roleMapper.selectByNameZh(roleNameZh) != null) {
            return RespBean.error("角色中文名已存在，请确认后重新添加");
        }
        Role role = new Role();
        role.setName("ROLE_" + roleName.trim());
        role.setNameZh(roleNameZh.trim());
        // 健壮性判断
        if (roleMapper.insert(role) < 1) {
            return RespBean.error("角色添加失败，请刷新后重试");
        }
        return RespBean.success("角色添加成功");
    }

    @Override
    public List<Role> roleList() {
        return roleMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public RespBean deleteRoleByRid(Integer rid) {
        // 查询角色下有无用户;返回对象集合
        if (adminRoleMapper.selectByRid(rid).size() > 0) {
            return RespBean.error("角色下存在用户无法删除，请到操作员管理取消角色下所有用户再删除");
        }
        // 查询角色下是否有权限，返回集合；集合为空代表没有权限，角色直接删除；有权限，先删权限再删角色
        if (menuRoleMapper.selectByRid(rid).size() == 0) {
            // 没有权限直接删除角色
            if (roleMapper.deleteRoleByRid(rid)) {
                return RespBean.success("角色删除成功");
            }
            return RespBean.error("角色删除失败，请刷新后重试!!!!!!!!!!!");
        } else {
            // 判断删除权限是否成功，返回Boolean类型，为真则删除成功；然后删除角色
            if (menuRoleMapper.deleteByRid(rid)) {
                // 判断角色是否删除成功，返回Boolean值，
                if (roleMapper.deleteRoleByRid(rid)) {
                    return RespBean.success("角色删除成功");
                }
                return RespBean.error("角色删除失败，请刷新后重试!!!!!!!!!!!");
            }
            return RespBean.error("角色删除失败，请刷新后重试!!!!!!!!!!!");
        }
    }

    /**
     * 根据角色id查询菜单id集合
     *
     * @param rid
     * @return
     */
    @Override
    public List<Integer> getMidByRid(Integer rid) {
        return super.baseMapper.getMidByRid(rid);
    }

    /**
     * 删除角色及该角色所属角色菜单表中的数据
     * @param id
     * @return
     */
    @Override
    public boolean removeById(Serializable id) {
        //删除该角色所属角色菜单表中的数据
        super.baseMapper.removeMenuByRids(Collections.singletonList(id));
        super.baseMapper.removeAdminByRids(Collections.singletonList(id));
        //删除该角色
        return super.removeById(id);
    }

    /**
     * 批量删除角色
     * @param idList
     * @return
     */
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        //删除关系表用户角色数据
        super.baseMapper.removeAdminByRids(idList);
        //删除关系表角色菜单数据
        super.baseMapper.removeMenuByRids(idList);
        return super.removeByIds(idList);
    }
}
